package com.intpro.demo.Utils.MyAnnotations;

import com.intpro.demo.DAO.AdminUser;
import com.intpro.demo.DAO.User;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

@Component
public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentResolver
{

	private static final String USER = "user";
	private static final String ADMIN = "admin";

	@Override
	public boolean supportsParameter(MethodParameter methodParameter)
	{
		//检查该解析器是否支持参数类型 ，如果方法的参数里有CurrentUser注解 并且参数类型 是UserDTO 则返回true
		return methodParameter.hasParameterAnnotation(CurrentUser.class) &&
				(methodParameter.getParameterType().isAssignableFrom(User.class) ||
						methodParameter.getParameterType().isAssignableFrom(AdminUser.class));
	}


	@Override
	public Object resolveArgument(MethodParameter methodParameter,
			ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest,
			WebDataBinderFactory webDataBinderFactory) throws Exception
	{
		//从request 中获取user信息
		if (methodParameter.getParameterType().isAssignableFrom(User.class))
		{
			User userDTO = (User) nativeWebRequest
					.getAttribute(USER, RequestAttributes.SCOPE_REQUEST);
			//不成功则跑出异常
			if (userDTO != null)
			{
				return userDTO;
			}
		}else if (methodParameter.getParameterType().isAssignableFrom(AdminUser.class))
		{
			AdminUser userDTO = (AdminUser) nativeWebRequest
					.getAttribute(ADMIN, RequestAttributes.SCOPE_REQUEST);
			//不成功则跑出异常
			if (userDTO != null)
			{
				return userDTO;
			}
		}
		throw new MissingServletRequestPartException(USER);
	}
}